﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessObject;
using QLTV_V2.BussinessLogic;

namespace QLTV_V2
{
    public partial class FrmInMaDKCB : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmInMaDKCB()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        SachBUS sachBUS = new SachBUS();
        private void FrmInMaDKCB_Load(object sender, EventArgs e)
        {
            dgv_DauSach.DataSource = getSource_DauSach();
        }

      
        
        private void txt_NhanDe_TextChanged(object sender, EventArgs e)
        {
            dgv_DauSach.DataSource = getSource_DauSach();
        }

        private void txt_TacGia_TextChanged(object sender, EventArgs e)
        {
            dgv_DauSach.DataSource = getSource_DauSach();
        }

        private void dgv_DauSach_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            string madausach = dgv_DauSach.Rows[dong].Cells["MaDauSach2"].Value.ToString();
            dgv_ChonDKCB.DataSource = sachBUS.getSource_BanSach(madausach);
        }
        #region chọn
        private void btn_ChonTatCa_Click(object sender, EventArgs e)
        {
            int count = dgv_ChonDKCB.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_ChonDKCB.Rows[i].Cells["chon"].Value = true;
        }

        private void btn_BoChon_Click(object sender, EventArgs e)
        {
            int count = dgv_ChonDKCB.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_ChonDKCB.Rows[i].Cells["chon"].Value = false;
        }

        private void btn_SelectAll_Click(object sender, EventArgs e)
        {
            int count = dgv_BanSach.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_BanSach.Rows[i].Cells["chon1"].Value = true;
        }

        private void btn_deSelect_Click(object sender, EventArgs e)
        {
            int count = dgv_BanSach.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_BanSach.Rows[i].Cells["chon1"].Value = false;
        }
        #endregion
        //Add dgv_ChonDKCB --> dgv_BanSach
        private void btn_Chon_Click(object sender, EventArgs e)
        {
            dgv_BanSach.CurrentCell = dgv_BanSach.Rows[0].Cells[0];
            int count = dgv_ChonDKCB.Rows.Count - 1;
            for (int i = 0; i < count; i++)
            {
                if (Convert.ToBoolean(dgv_ChonDKCB.Rows[i].Cells["chon"].Value) == true)
                {
                    int index = dgv_BanSach.Rows.Add();
                    DataGridViewRow newRow = dgv_BanSach.Rows[index];
                    newRow.Cells["chon1"].Value = true;
                    newRow.Cells["MaBanSach1"].Value = dgv_ChonDKCB.Rows[i].Cells["MaBanSach"].Value.ToString();
                    newRow.Cells["TenDauSach1"].Value = dgv_DauSach.Rows[dgv_DauSach.CurrentCell.RowIndex].Cells["TenDauSach"].Value.ToString();
                    newRow.Cells["MaTacGia1"].Value = dgv_DauSach.Rows[dgv_DauSach.CurrentCell.RowIndex].Cells["MaTacGia"].Value.ToString();
                    newRow.Cells["KHPhanLoai1"].Value = dgv_DauSach.Rows[dgv_DauSach.CurrentCell.RowIndex].Cells["KHPhanLoai"].Value.ToString();
                    newRow.Cells["NamXB1"].Value = Convert.ToInt32(dgv_DauSach.Rows[dgv_DauSach.CurrentCell.RowIndex].Cells["NamXB"].Value);
                    newRow.Cells["NgayCN1"].Value = Convert.ToDateTime(dgv_ChonDKCB.Rows[i].Cells["NgayCN"].Value);
                    newRow.Cells["TinhTrang1"].Value = dgv_ChonDKCB.Rows[i].Cells["TinhTrang"].Value.ToString();
                }
            }    
        }
        //lấy souce dgv_DauSach
        DataTable getSource_DauSach()
        {
            string sql = "select DauSach.MaLoaiSach,TenLoaiSach,MaDauSach as MaDauSach2, TenDauSach,TacGia.MaTacGia, TenTacGia,NamXB,KHPhanLoai " +
                         "from DauSach join LoaiSach on DauSach.MaLoaiSach=LoaiSach.MaLoaiSach " +
                         "join TacGia on DauSach.MaTacGia=TacGia.MaTacGia " +
                         "where MaDauSach is not null ";
            if (txt_NhanDe.Text != "")
                sql += " and TenDauSach like N'%" + txt_NhanDe.Text + "%' ";
            if (txt_TacGia.Text != "")
                sql += "and TenTacGia like N'%" + txt_TacGia.Text + "%' ";
            return conn.GetDataTable(sql);
        }


        public DataTable getDataTable_NhanGay()
        {
            dgv_BanSach.CurrentCell = dgv_BanSach.Rows[0].Cells[0];
                DataTable dtb = new DataTable();
                DataRow newRow = null;
                dtb.Columns.Add(new DataColumn("MaBanSach", typeof(string)));
                dtb.Columns.Add(new DataColumn("KHPhanLoai", typeof(string)));
                dtb.Columns.Add(new DataColumn("MaTacGia", typeof(string)));
                dtb.Columns.Add(new DataColumn("NamXB", typeof(Int32)));
                for (int i = 0; i < dgv_BanSach.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dgv_BanSach.Rows[i].Cells["chon1"].Value) == true)
                    {
                        newRow = dtb.NewRow();
                        newRow["MaBanSach"] = dgv_BanSach.Rows[i].Cells["MaBanSach1"].Value.ToString();
                        newRow["KHPhanLoai"] = dgv_BanSach.Rows[i].Cells["KHPhanLoai1"].Value.ToString();
                        newRow["MaTacGia"] = dgv_BanSach.Rows[i].Cells["MaTacGia1"].Value.ToString();
                        newRow["NamXB"] = Convert.ToInt32(dgv_BanSach.Rows[i].Cells["NamXB1"].Value);
                        dtb.Rows.Add(newRow);
                    }
                }
            
            return dtb;
        }
        public DataTable getDataTable_InDKCB()
        {
            dgv_BanSach.CurrentCell = dgv_BanSach.Rows[0].Cells[0];
            DataTable dtb = new DataTable();
            DataRow newRow = null;
            dtb.Columns.Add(new DataColumn("MaLoaiSach", typeof(string)));
            dtb.Columns.Add(new DataColumn("DKCB", typeof(string)));
            for (int i = 0; i < dgv_BanSach.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dgv_BanSach.Rows[i].Cells["chon1"].Value) == true)
                {
                    string mabansach = dgv_BanSach.Rows[i].Cells["MaBanSach1"].Value.ToString();
                    string sql = "select LEFT(MaBanSach,LEN(MaBanSach)-10) as MaLoaiSach, RIGHT(MaBanSach,9)as DKCB " +
                             "from BanSach " +
                             "where MaBanSach='"+mabansach+"'";
                    DataRow dr = conn.GetDataTable(sql).Rows[0];
                    newRow = dtb.NewRow();
                    newRow["MaLoaiSach"] = dr["MaLoaiSach"].ToString();
                    newRow["DKCB"] = dr["DKCB"].ToString();
                    dtb.Rows.Add(newRow);
                }
            }

            return dtb;
        }
        public DataTable getDataTable_InMaVach()
        {
            dgv_BanSach.CurrentCell = dgv_BanSach.Rows[0].Cells[0];
            DataTable dtb = new DataTable();
            DataRow newRow = null;
            dtb.Columns.Add(new DataColumn("MaBanSach", typeof(string)));
            dtb.Columns.Add(new DataColumn("BarCode", typeof(Object)));
            for (int i = 0; i < dgv_BanSach.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dgv_BanSach.Rows[i].Cells["chon1"].Value) == true)
                {
                    string mabansach = dgv_BanSach.Rows[i].Cells["MaBanSach1"].Value.ToString();
                    newRow = dtb.NewRow();
                    newRow["MaBanSach"] = mabansach;
                    dtb.Rows.Add(newRow);
                }
            }

            return dtb;
        }

        public DataTable getDataTable_InPhichMoTa()
        {
            dgv_BanSach.CurrentCell = dgv_BanSach.Rows[0].Cells[0];
            DataTable dtb = new DataTable();
            DataRow newRow = null;
            dtb.Columns.Add(new DataColumn("TenDauSach", typeof(string)));
            dtb.Columns.Add(new DataColumn("TenTacGia", typeof(string)));
            dtb.Columns.Add(new DataColumn("DongTacGia", typeof(string)));
            dtb.Columns.Add(new DataColumn("LanXB", typeof(string)));
            dtb.Columns.Add(new DataColumn("TenNXB", typeof(string)));
            dtb.Columns.Add(new DataColumn("NamXB", typeof(string)));
            dtb.Columns.Add(new DataColumn("SoTrang", typeof(string)));
            dtb.Columns.Add(new DataColumn("KhoGiay", typeof(string)));
            dtb.Columns.Add(new DataColumn("KHPhanLoai", typeof(string)));
            dtb.Columns.Add(new DataColumn("MaBanSach", typeof(string)));
            for (int i = 0; i < dgv_BanSach.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dgv_BanSach.Rows[i].Cells["chon1"].Value) == true)
                {
                    string mabansach = dgv_BanSach.Rows[i].Cells["MaBanSach1"].Value.ToString();
                    string sql = "select TenDauSach, TenTacGia, DongTacGia, LanXB, TenNXB,NamXB, SoTrang, KhoGiay, KHPhanLoai, MaBanSach "+
                                "from BanSach a join DauSach b on a.MaDauSach=b.MaDauSach "+
                                "join TacGia c on b.MaTacGia=c.MaTacGia "+
                                "join NhaXuatBan d on b.MaNXB=d.MaNXB " +
                                "where MaBanSach='" + mabansach + "'";
                    DataRow dr = conn.GetDataTable(sql).Rows[0];
                    newRow = dtb.NewRow();
                    newRow["TenDauSach"] = dr["TenDauSach"].ToString();
                    newRow["TenTacGia"] = dr["TenTacGia"].ToString();
                    newRow["DongTacGia"] = dr["DongTacGia"].ToString();
                    newRow["LanXB"] = dr["LanXB"].ToString();
                    newRow["TenNXB"] = dr["TenNXB"].ToString();
                    newRow["NamXB"] = dr["NamXB"].ToString();
                    newRow["SoTrang"] = dr["SoTrang"].ToString();
                    newRow["KhoGiay"] = dr["KhoGiay"].ToString();
                    newRow["KHPhanLoai"] = dr["KHPhanLoai"].ToString();
                    newRow["MaBanSach"] = dr["MaBanSach"].ToString();
                    dtb.Rows.Add(newRow);
                }
            }

            return dtb;
        }
     
        private void btn_NhanGay_Click(object sender, EventArgs e)
        {
            FrmInDKCB frm = new FrmInDKCB();
            frm.dtb = getDataTable_NhanGay();
            frm.dk = 1;
            frm.ShowDialog();
        }

        private void btn_InMaDKCB_Click(object sender, EventArgs e)
        {
            FrmInDKCB frm = new FrmInDKCB();
            frm.dtb = getDataTable_InDKCB();
            frm.dk = 2;
            frm.ShowDialog();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            FrmInDKCB frm = new FrmInDKCB();
            frm.dtb = getDataTable_InMaVach();
            frm.dk = 3;
            frm.ShowDialog();
        }

        private void btn_InPhich_Click(object sender, EventArgs e)
        {
            FrmInDKCB frm = new FrmInDKCB();
            frm.dtb = getDataTable_InPhichMoTa();
            frm.dk = 4;
            frm.ShowDialog();
        }
//Tao ma vach
        private Bitmap CreatBarcode(string data)
        {
            Bitmap barCode = new Bitmap(1, 1);
            Font threeOfNine = new Font("Free 3 of 9", 60, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            Graphics graphics = Graphics.FromImage(barCode);

            SizeF dataSize = graphics.MeasureString(data, threeOfNine);

            barCode = new Bitmap(barCode, dataSize.ToSize());

            graphics = Graphics.FromImage(barCode);

            graphics.Clear(Color.White);

            graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), 0, 0);

            graphics.Flush();

            threeOfNine.Dispose();

            graphics.Dispose();

            return barCode;
        }
       
        private Image Image(Bitmap barCode)
        {
            throw new NotImplementedException();
        }
        
        private void dgv_BanSach_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_BanSach);
        }

        private void dgv_DauSach_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_DauSach);
        }

        private void dgv_ChonDKCB_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_ChonDKCB);
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không?", "Hỏi",
         MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

    }
}
